﻿#include "ThreadMsgManager.h"
#include <StdClassFun.h>
#include <iostream>
#include <QMutexLocker>

ThreadMsgManager::ThreadMsgManager(const QHash<QString, QDateTime>& fileChangeInfo
	, const QString& fileParh): fileChangeInfo{ fileChangeInfo },
	fileParh{ fileParh } {}

void ThreadMsgManager::run( ) {
	QMutexLocker locker(getShowMutexObj( ));
	QFileInfo info(fileParh);
	if( fileChangeInfo.isEmpty( ) ) {
		if( !info.exists( ) ) {
			std::cout <<(info.absoluteFilePath(  )).toLocal8Bit( ).constData( ) << QString("\n\t\t路径不合法").toLocal8Bit( ).constData( ) << std::endl;
		}
	} else {
		QHash<QString, QDateTime>::iterator iterator = fileChangeInfo.begin( );
		QHash<QString, QDateTime>::iterator end = fileChangeInfo.end( );
		// 配置输出格式
		auto chars = getFileOutForm( );
		bool useFileFort = true;
		if( chars->isEmpty( ) )
			useFileFort = false;
		// 配置过滤器
		bool userFilter = !getNormalFilter( )->isEmpty( );
		for( ;iterator != end;++iterator ) {
			bool filterOk = true;
			if( userFilter )
				if( !doNormalFilter(iterator.key( )) )
					filterOk = false;
			if( filterOk )
				if( useFileFort ) {
					auto&& fileOutForm = doFileOutForm(iterator.key( ));
					std::cout << fileOutForm.toLocal8Bit( ).constData( ) << std::endl;
				} else {
					QString buff("------------\n发现目录\t\t\n" + fileParh + "\n\t\t======================\n\t\n" + iterator.key( ) + "\n>>=\t\t\t文件过期\n\t\t更改日期为\n\t\t\n" + iterator.value( ).toString("yyyy 年 MM 月 dd 日 hh 小时 mm 分钟 ss 秒钟") + "\n\t======================\n------------\n");
					std::cout << buff.toLocal8Bit( ).constData( ) << std::endl;
				}
		}
	}
	emit releaseEmit(this);
}
